package com.gametwo;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.view.SurfaceHolder;

public class GameThread extends Thread {
	private SurfaceHolder mHolder = null; // Holder reference
	boolean running = false; // exits the thread when set false
	private long mLastTime = 0; // used for maintaining FPS
	private int width, height, fps, ifps; // store width and height of canvas
											// and how many frames are rendered
											// per second
	Sprite ball,ball1,ball2; 
	private Paint fpaint = new Paint();
	private Paint paint = new Paint();
	private Rect rect = new Rect(0, 0, width, height);

	public GameThread(SurfaceHolder surfaceholder, Context context,
			Handler handler) {
		this.mHolder = surfaceholder; // only store holder for now
		ifps = fps = 40;
		ball = new Sprite(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ball));
		ball1 = new Sprite(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ball1));
		ball2 = new Sprite(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ball2));
		ball.x = 100;
		ball.y = 100;
		ball.vx = 12;
		ball.vy = 8;
		ball1.x = 50;
		ball1.y = 50;
		ball1.vx = 18;
		ball1.vy = 10;
		ball2.x = 150;
		ball2.y = 150;
		ball2.vx = 20;
		ball2.vy = 12;
		fpaint.setColor(Color.WHITE);
		paint.setColor(Color.BLACK);

	}

	//public GameThread(Context context) {
		//ball = new Sprite(BitmapFactory.decodeResource(context.getResources(),
			//	R.drawable.ball));
		/*ball1 = new Sprite(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ball1));
		ball2 = new Sprite(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ball2));*/
	//	ball.x = 100;
		//ball1.x = 50;
		//ball2.x = 20;
		//ball.y = 100;
		//ball1.y = 50;
		//ball2.y = 20;
		//ball.vx = 12;
		//ball1.vx = 15;
		//ball2.vx = 17;
		//ball.vy = 8;
		//ball1.vy = 5;
		//ball2.vy = 7;
		//ifps = fps = 0;
		//fpaint.setColor(Color.WHITE);
		//paint.setColor(Color.BLACK);
	//}

	// init vars here
	public void doStart() {
		synchronized (mHolder) {
			mLastTime = System.currentTimeMillis() + 100;
		}
	}

	@Override
	public void run() {
		while (running) {
			Canvas c = null;
			try {
				c = mHolder.lockCanvas(); // grab canvas from the holder
				if (width == 0) { // store screen width and height
					width = c.getWidth();
					height = c.getHeight();
				}

				synchronized (mHolder) {
					long now = System.currentTimeMillis();

					update(); // this method updates game logic every frame
					draw(c); // this method renders the current frame

					// logic for maintaining FPS
					ifps++;
					if (now > (mLastTime + 1000)) {
						mLastTime = now;
						fps = ifps;
						ifps = 0;
					}
				}
			} finally {
				if (c != null) {
					// finally whatever the scenario must release the canvas
					mHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}

	private void update() {
		ball.x += ball.vx;
		ball.y -= ball.vy;

		if (ball.x < 0 || ball.x + ball.getWidth() > width)
			ball.vx = -ball.vx;

		if (ball.y < 0 || ball.y + ball.getHeight() > height)
			ball.vy = -ball.vy;

		ball1.x -= ball1.vx;
		ball1.y += ball1.vy;

		if (ball1.x < 0 || ball1.x + ball1.getWidth() > width)
			ball1.vx = -ball1.vx;

		if (ball1.y < 0 || ball1.y + ball1.getHeight() > height)
			ball1.vy = -ball1.vy;

		ball2.x += ball2.vx;
		ball2.y -= ball2.vy;

		if (ball2.x < 0 || ball2.x + ball2.getWidth() > width)
			ball2.vx = -ball2.vx;

		if (ball2.y < 0 || ball2.y + ball2.getHeight() > height)
			ball2.vy = -ball2.vy;
		
		if(ball.y + ball.getHeight() >= ball1.y) {
			  if(ball.x + ball.getWidth() > ball1.x && ball.x < ball1.x + ball1.getWidth()) {
			    ball.vy = -ball.vy;
			    ball1.vy = -ball1.vy;
			  }
			}
		if(ball.y + ball.getHeight() >= ball2.y) {
			  if(ball.x + ball.getWidth() > ball2.x && ball.x < ball2.x + ball2.getWidth()) {
			    ball.vy = -ball.vy;
			    ball2.vy = -ball2.vy;
			  }
			}
		if(ball1.y + ball1.getHeight() >= ball2.y) {
			  if(ball1.x + ball1.getWidth() > ball2.x && ball1.x < ball2.x + ball2.getWidth()) {
			    ball1.vy = -ball1.vy;
			    ball2.vy = -ball2.vy;
			  }
			}
		
		

	}

	private void draw(Canvas c) {
		
		//ball1.draw(c);
		//ball2.draw(c);
		c.drawColor(Color.BLACK);
		c.drawRect(rect, paint);
		ball.draw(c);
		ball1.draw(c);
		ball2.draw(c);
	}
}
